import { Injectable, Post } from '@nestjs/common';
import { CreateUserDto } from './dto/create-user.dto';
import { UpdateUserDto } from './dto/update-user.dto';
import { InjectEntityManager, InjectRepository, getRepositoryToken } from '@nestjs/typeorm';
import { EntityManager, Repository } from 'typeorm';
import { User } from './entities/user.entity';

@Injectable()
export class UserService {

 /*Repository方式   
  @InjectRepository(User)
  private userRepository: Repository<User>;

  findAll() {
    return this.userRepository.find();
  } 
*/
  
  //注入EntityManager对象
  @InjectEntityManager()
  private manager: EntityManager;

  
  //Dto
  create(createUserDto: CreateUserDto) {
      this.manager.save(User,createUserDto)
  }

  findAll() {
    // this.userRepository.find();
    return this.manager.find(User);
  }
  

  findOne(id: number) {
    return this.manager.findOne(User,{
      where: { id }
    })
  }

  update(id: number, updateUserDto: UpdateUserDto) {
    // this.manager.update(User,id,updateUserDto)
    this.manager.save(User,{
      id,
      ...updateUserDto
    })
  }

  remove(id: number) {
    this.manager.delete(User, id);
  }


}
